//
//  HPChoiceButtonView.m
//  Food choices
//
//  Created by xalo on 16/3/5.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#define KHor 2

#import "HPChoiceButtonView.h"

//弹出选择页面静态变量


@interface HPChoiceButtonView ()

@end


//主引擎
@implementation HPChoiceButtonView

//初始化
-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        self.buttonView = [[UIView alloc] initWithFrame:frame];
        self.buttonView.backgroundColor = [UIColor colorWithRed:0.928 green:0.982 blue:0.961 alpha:1.000];
        
        _areaBtn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
        _areaBtn.backgroundColor = [UIColor colorWithRed:0.761 green:0.946 blue:0.883 alpha:1.000];
        
        _filterBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _filterBtn.backgroundColor = [UIColor colorWithRed:0.761 green:0.946 blue:0.883 alpha:1.000];
        
        _areaBtn.frame = CGRectMake(0, frame.size.height-44, (kScreenWidth - KHor)/2, 44);
        _filterBtn.frame = CGRectMake(kScreenWidth/2 + KHor, frame.size.height-44 , (kScreenWidth - KHor)/2, 44);
        
        [_areaBtn setTitle:@"区域选择" forState:(UIControlStateNormal)];
        [_filterBtn setTitle:@"附近选择" forState:(UIControlStateNormal)];
        [_areaBtn setTintColor:[UIColor orangeColor]];
        [_filterBtn setTintColor:[UIColor orangeColor]];
        
        [_areaBtn addTarget:self action:@selector(areaAction) forControlEvents:(UIControlEventTouchUpInside)];
        [_filterBtn addTarget:self action:@selector(filterAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self.buttonView addSubview:_areaBtn];
        [self.buttonView addSubview:_filterBtn];
        

        //初始化要展示文字的数组
        self.filterArray = [NSArray arrayWithObjects:@"500",@"1000",@"2000",@"3000", nil];
        
        self.areaSelected = NO;
        self.filterSelected = NO;
        
    }
    return self;
}


#pragma mark -- button对应点击方法实现
//areaBtn的Action
-(void)areaAction
{
    //先判定范围选择页面是否在显示
    if (_filterSelected)
    {
        [self closeChoiceView];
        
    }
    
    
    if (_areaSelected) {
        
        [self closeChoiceView];

        
    }else{

        [self.delegete addToSupView:self.areaView];
        
        _areaSelected = !_areaSelected;
    }
    

}


//筛选Action
-(void)filterAction
{
    //先判断区域选择页面是否在显示
    if (_areaSelected)
    {
        [self closeChoiceView];
    }
    
    if (_filterSelected) {
        
        [self closeChoiceView];
    }
    else{
        
        [self.delegete addToSupView:self.filterView];
        
        _filterSelected = !_filterSelected;
    }
    

}



-(void)creatChoiceViewWithY:(CGFloat )y
{
    NSLog(@"创建视图执行");
    CGFloat height;

    height = kScreenHeight - y - 64 - 49;
    
    _areaView = [[ChoiceView alloc] initWithFrame:(CGRectMake(0, y, kScreenWidth, kScreenHeight)) needRightTable:YES modelArray:self.districtArray];
    
    _filterView = [[ChoiceView alloc] initWithFrame:(CGRectMake(0, y, kScreenWidth, kScreenHeight)) needRightTable:NO modelArray:self.filterArray];
    
    [self addGestureForView:_areaView];
    [self addGestureForView:_filterView];
}


//添加手势
-(void)addGestureForView:(ChoiceView *)choiceView
{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeChoiceView)];
    [choiceView.lucentView addGestureRecognizer:tap];
}

//判断并返回当前显示的Choice视图
-(ChoiceView *)getNowView
{
    if (_filterSelected)
    {
        _filterSelected = !_filterSelected;
        return _filterView;
    }
    
    if (_areaSelected) {
        _areaSelected = !_areaSelected;
        return _areaView;
    }

    return nil;
}

//关闭视图
-(void)closeChoiceView
{
    ChoiceView * choiceView = [self getNowView];
    
    [choiceView removeFromSuperview];
    
    [self.delegete closeChoiceView];
    
    
}



@end
